package com.tf_cut.admin.seller.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.tf_cut.admin.annotation.Decrypt;
import com.tf_cut.admin.annotation.Encrypt;
import com.tf_cut.admin.backend.api.BackEndApi;
import com.tf_cut.admin.common.pojo.PageInfo;
import com.tf_cut.admin.common.pojo.Result;
import com.tf_cut.admin.seller.vo.SellerRefAgentVo;

import lombok.extern.slf4j.Slf4j;


@RestController
@RequestMapping("/seller/ref/")
@Slf4j
public class SellerRefAgentController {
	
	@Autowired
	private BackEndApi sellerApi;
	
	// 进入关联清单页面
	@GetMapping("reflist")
    public ModelAndView refList() {
        return new ModelAndView("seller/ref/reflist", "seller", null);
    }
	
	// 进入关联页面
	@GetMapping("refinsert")
    public ModelAndView refInsert() {
        return new ModelAndView("seller/ref/refinsert", "seller", null);
    }

	// 进入关联更新页面
	@GetMapping("refupdate")
    public ModelAndView refupdate(String relationId,String agentName,String sellerName) {
		SellerRefAgentVo avo = null == relationId ? 
				new SellerRefAgentVo() : 
					JSON.parseObject(sellerApi.getSellerRef(relationId),SellerRefAgentVo.class);
		avo.setAgentName(agentName);
		avo.setSellerName(sellerName);
        return new ModelAndView("seller/ref/refupdate", "seller", avo);
    }
	
	// 关联清单页面翻页
	@SuppressWarnings("unchecked")
	@PostMapping("reflist/page")
	@Decrypt
    @Encrypt
    public Result<PageInfo<SellerRefAgentVo>> page(SellerRefAgentVo avo) {
	
        String json = JSON.toJSONString(avo);
        json = sellerApi.findListSellerRefAgent(json);
        return Result.of(JSON.parseObject(json, PageInfo.class));
    }
	
	@SuppressWarnings("unchecked")
	@PostMapping("saveRef")
	@Decrypt
    @Encrypt
    public Result<SellerRefAgentVo> saveRef(SellerRefAgentVo avo) {
		//修改关系时报“重复绑定代理商”，加入判断条件处理
		if(avo.getRelationId()!=null && avo.getRelationId().trim().length()>0){  //修改关联
			String json = JSON.toJSONString(avo);
			json = sellerApi.saveSellerRefAgent(json);
			return JSON.parseObject(json, Result.class);
		}

		SellerRefAgentVo newvo=new SellerRefAgentVo();
		newvo.setSellerId(avo.getSellerId());
		String str= sellerApi.findListSellerRefAgent(JSON.toJSONString(newvo));
		if(avo.getRelationId()==null && str.indexOf("relationId")==-1){  //新增关联
			avo.setRefState("Y");
			String json = JSON.toJSONString(avo);
			json = sellerApi.saveSellerRefAgent(json);
			return JSON.parseObject(json, Result.class);
		}else if(avo.getRelationId()==null && str.indexOf("relationId")>0){  //不能关联多个
			Result<SellerRefAgentVo> result = Result.of(avo);
			result.setMsg("重复关联代理商");
			result.setFlag(false);
			return result;
		}
		return null;
    }
	
	// 冻结（Y）/删除（D）/解冻（N） 
	@SuppressWarnings("unchecked")
	@PostMapping("operation")
	@Decrypt
    @Encrypt
	public Result<Boolean> operation(SellerRefAgentVo avo) {
		return JSON.parseObject(sellerApi.operationRef(avo.getRelationId(),avo.getRefState()),Result.class);
	}
}
